﻿Imports StedySoft.SenseSDK
Public Class frmTracking

    Dim WithEvents objFolderPath As New SensePanelItem
    Dim WithEvents objLineWidth As New SensePanelNumericItem
    Dim WithEvents objColor As New SensePanelComboItem
    Dim WithEvents objSavePoint As New SensePanelNumericItem
    Dim WithEvents objTagName As New SensePanelTextboxItem

    Dim SIPOffset As Integer = 0

    Private Sub frmTracking_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        lstMain.Clear()
        objFolderPath.PrimaryText = "KML Folder"
        objLineWidth.PrimaryText = "Line Width"
        objLineWidth.Value = 5
        objLineWidth.Maximum = 20
        objLineWidth.Minimum = 1

        objSavePoint.PrimaryText = "Record Position Information"
        objSavePoint.SecondaryText = String.Format("Every {0} points", CInt(Glob.MobileSettings.RecordPositionEvery))
        objSavePoint.Value = 50
        objSavePoint.Maximum = 1000
        objSavePoint.Minimum = 10

        objTagName.LabelText = "File Tag Name"

        objColor.Items.Clear()
        objColor.Items.Add(New SensePanelComboItem.Item("Red", "Red"))
        objColor.Items.Add(New SensePanelComboItem.Item("Pink", "Pink"))
        objColor.Items.Add(New SensePanelComboItem.Item("Blue", "Blue"))
        objColor.Items.Add(New SensePanelComboItem.Item("Black", "Black"))
        objColor.Items.Add(New SensePanelComboItem.Item("Yellow", "Yellow"))
        objColor.Items.Add(New SensePanelComboItem.Item("Brown", "Brown"))
        objColor.Items.Add(New SensePanelComboItem.Item("White", "White"))
        objColor.LabelText = "Line Color"

        lstMain.AddItem(objFolderPath)
        lstMain.AddItem(objTagName)
        lstMain.AddItem(objLineWidth)
        lstMain.AddItem(objColor)
        lstMain.AddItem(objSavePoint)


        objFolderPath.SecondaryText = Glob.MobileSettings.KMLFolder
        objLineWidth.Value = CInt(Glob.MobileSettings.LineWidth)
        objColor.SelectedValue = Glob.MobileSettings.LineColor
        objSavePoint.Value = CInt(Glob.MobileSettings.RecordPositionEvery)
        objTagName.Text = Glob.MobileSettings.FileTag
    End Sub

    Private Sub objFolderPath_OnClick(ByVal Sender As Object) Handles objFolderPath.OnClick
        Dim objFolderSelector As New frmDirectorySelect
        objFolderSelector.ShowDialog()
        If objFolderSelector.DialogResult = Windows.Forms.DialogResult.OK Then
            objFolderPath.SecondaryText = objFolderSelector.SelectedPath
        End If
    End Sub

    Private Sub objSavePoint_OnValueChanged(ByVal Sender As Object, ByVal Value As Integer) Handles objSavePoint.OnValueChanged
        objSavePoint.SecondaryText = String.Format("Every {0} points", Value)
    End Sub

    Private Sub mnBack_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnBack.Click
        Me.DialogResult = Windows.Forms.DialogResult.Cancel
        Me.Close()
    End Sub

    Private Sub objTagName_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles objTagName.GotFocus
        InputPanel1.Enabled = True
        ReLayout()
    End Sub

    Private Sub objTagName_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles objTagName.LostFocus
        InputPanel1.Enabled = False
        ReLayout()
    End Sub

    Private Sub mnStartTracking_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnStartTracking.Click
        Glob.MobileSettings.FileTag = objTagName.Text
        Glob.MobileSettings.KMLFolder = objFolderPath.SecondaryText
        Glob.MobileSettings.LineColor = objColor.SelectedValue
        Glob.MobileSettings.LineWidth = objLineWidth.Value
        Glob.MobileSettings.RecordPositionEvery = objSavePoint.Value
        Me.DialogResult = Windows.Forms.DialogResult.OK
        Me.Close()
    End Sub

    Private Sub InputPanel1_EnabledChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles InputPanel1.EnabledChanged
        ReLayout()
    End Sub

    Private Sub ReLayout()
        Dim Hwnd = GetForegroundWindow()
        Dim StBuilder As New System.Text.StringBuilder
        StBuilder.Capacity = 8
        GetWindowText(Hwnd, StBuilder, StBuilder.Capacity)


        If StBuilder.ToString.ToLower = "frmtrip" Then
            If InputPanel1.Enabled Then
                Dim IItem As SenseListControl.ISenseListItem = lstMain.FocusedItem
                Dim R As Rectangle = IItem.ClientRectangle
                R.Offset(0, lstMain.Bounds.Top)
                If R.Bottom > InputPanel1.VisibleDesktop.Height Then
                    SIPOffset = Math.Abs(InputPanel1.VisibleDesktop.Height - R.Bottom)
                    lstMain.ScrollList(-SIPOffset)
                    lstMain.Invalidate()
                End If
            Else
                If SIPOffset <> 0 Then
                    lstMain.ScrollList(SIPOffset)
                    lstMain.Invalidate()
                    SIPOffset = 0
                End If
            End If
        End If
    End Sub
End Class